/**
 * 对象或Map转数组
 * @param map Map 或 对象
 * @param keyName 转换成的项中的key属性 默认value
 * @param valueName 转换成的项中的value属性 默认name
 */
function mapToArray(map = {}, keyName = 'value', valueName = 'name') {
  const arr = []
  if (map instanceof Map) {
    for (const key of map.keys()) {
      const curValue = map.get(key)
      const value = typeof curValue === 'object' ? curValue[valueName] : curValue
      arr.push({
        [keyName]: key,
        [valueName]: value,
      })
    }
  } else {
    for (const key in map) {
      const curValue = map[key]
      const value = typeof curValue === 'object' ? curValue[valueName] : curValue
      arr.push({
        [keyName]: key,
        [valueName]: value,
      })
    }
  }

  return arr
}

/**
 * 对象数组转Map
 * @param arr 源数组
 * @param key 主键 默认value
 * @param valueKey 可选，源对象值为对象时，要作为值的属性名，空时时为整个数组项
 */
function arrayToMap(arr = [], key = 'value', valueKey) {
  const map = new Map()
  arr?.forEach?.(d => {
    map.set(d[key], valueKey ? d[valueKey] : d)
  })
  return map
}

/**
 * 数组转Object
 * @param arr 源数组
 * @param key 主键 默认value
 * @param valueKey 可选，源对象值为对象时，要作为值的属性名，空时时为整个数组项
 */
function arrayToObj(arr = [], key = 'value', valueKey) {
  const map = {}
  arr?.forEach(d => {
    map[d[key]] = valueKey ? d[valueKey] : d
  })
  return map
}

/**
 * 获取随机id
 */
function getRandomId(key) {
  return new Date().getTime() + key + Math.ceil(Math.random() * 100000)
}

/**
 * 获取路径文件名（包含后缀）
 */
function baseName(path) {
  let lastIndex = path.lastIndexOf('/')
  if (lastIndex === -1) {
    lastIndex = path.lastIndexOf('\\')
  }
  if (lastIndex !== -1) {
    return path.substring(lastIndex + 1)
  }
}

/**
 * 获取文件扩展名
 */
function baseExt(fileName) {
  const lastIndex = fileName.lastIndexOf('.')
  if (lastIndex !== -1) {
    return fileName.substring(lastIndex + 1)
  }
}

/**
 * 获取元素相对Top
 */
function getElementTop(element) {
  let actualTop = element.offsetTop
  let current = element.offsetParent

  while (current !== null) {
    actualTop += current.offsetTop
    current = current.offsetParent
  }
  return actualTop
}

function getArrayRepeatMap(arr, attr) {
  const repeatTempMap = new Map()
  const hasAttr = typeof attr !== 'undefined'
  arr?.forEach((item, index) => {
    const key = hasAttr ? item[attr] : item
    const indexList = repeatTempMap.get(key)
    if (indexList) {
      indexList.push(index)
    } else {
      repeatTempMap.set(key, [ index ])
    }
  })
  const repeatMap = new Map()
  repeatTempMap.forEach((value, key) => {
    if (value.length > 1) {
      repeatMap.set(key, value)
    }
  })
  return repeatMap
}

function removeFromArray(arr, finder) {
  const toDelIndexList = []
  finder
    && arr?.forEach((item, i) => {
      if (finder(item, i)) {
        toDelIndexList.push(i)
      }
    })
  if (toDelIndexList.length) {
    toDelIndexList.forEach((toDelIndex, i) => {
      arr.splice(toDelIndex - i, 1)
    })
  }
}

export { baseName, baseExt, getRandomId, mapToArray, arrayToMap, arrayToObj, getElementTop, getArrayRepeatMap, removeFromArray }
